package com.hlyj.http.base.tool.lib_hlyj_base.utils

import android.os.Build
import java.time.Instant
import java.time.ZoneId
import java.util.Calendar

object TimeTools {
    fun isNextDay(timestamp1: Long, timestamp2: Long): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val date1 = Instant.ofEpochMilli(timestamp1).atZone(ZoneId.systemDefault()).toLocalDate()
            val date2 = Instant.ofEpochMilli(timestamp2).atZone(ZoneId.systemDefault()).toLocalDate()
            return date2 == date1.plusDays(1)
        } else {
            val calendar1 = Calendar.getInstance()
            val calendar2 = Calendar.getInstance()

            calendar1.timeInMillis = timestamp1
            calendar2.timeInMillis = timestamp2

            // 设置时间为当天的开始，以便只比较日期部分
            calendar1.set(Calendar.HOUR_OF_DAY, 0)
            calendar1.set(Calendar.MINUTE, 0)
            calendar1.set(Calendar.SECOND, 0)
            calendar1.set(Calendar.MILLISECOND, 0)

            calendar2.set(Calendar.HOUR_OF_DAY, 0)
            calendar2.set(Calendar.MINUTE, 0)
            calendar2.set(Calendar.SECOND, 0)
            calendar2.set(Calendar.MILLISECOND, 0)

            // 比较年份、月份和日期
            return calendar2.get(Calendar.YEAR) == calendar1.get(Calendar.YEAR) &&
                    calendar2.get(Calendar.MONTH) == calendar1.get(Calendar.MONTH) &&
                    calendar2.get(Calendar.DAY_OF_MONTH) == calendar1.get(Calendar.DAY_OF_MONTH) + 1
        }

    }

    // 获取今天零点毫秒数
    fun getTodayZeroTime(): Long {
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)
        return calendar.timeInMillis
    }
}